﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace WFCSharp_II
{
    public partial class WavPlayer : Form
    {
        private SoundPlayer wavPlayer = new SoundPlayer();
        public WavPlayer()
        {
            InitializeComponent();
        }

        private void openFileButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                fileNameText.Text = openFileDialog.FileName;
            }
        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            wavPlayer.SoundLocation = fileNameText.Text;
            progressBar.Visible = true;
            backgroundWorker.RunWorkerAsync();
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            wavPlayer.Play();
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                wavPlayer.Load();

                // Giả sử file lớn load lâu thêm 3 giây nữa
                for (int i = 0; i < 4; i++)
                {
                    System.Threading.Thread.Sleep(1000);
                    backgroundWorker.ReportProgress(25 * (i + 1));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "My Player", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar.Value = (e.ProgressPercentage);
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar.Visible = false;
        }

        private void fileNameText_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effect = DragDropEffects.Copy;
            else if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void fileNameText_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
                fileNameText.Text = fileNames[0];
            }
            else if (e.Data.GetDataPresent(DataFormats.Text))
                fileNameText.Text = (string) e.Data.GetData(DataFormats.Text);
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            label1.DoDragDrop(label1.Text, DragDropEffects.Copy);
        }
    }
}
